<?php
require_once BACKGROUND_COMMON_ACTION_SERVICE_DIR . '/default.php';

class background_order_update_action_service extends background_default_common_action_service
{
    protected static $_instance;

    public static function get_instance()
    {
        if (null === self::$_instance) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }


    protected function _current_page_process()
    {
        //var_dump(__METHOD__); exit();
                
        $result = array('status' => 0, 'message' => '', 'data' => array());
    
        //var_dump($_POST); exit();
	    $id          = init_user_data_key($_POST, 'id', 'int');
	    $shipping_id = init_user_data_key($_POST, 'shipping_id', 'int');
	    $shipping_sn = init_user_data_key($_POST, 'shipping_sn', 'str');
         
        //$id = null;
        if(empty($id)){
            $result['status']  = -400;
            $result['message'] = '请选择订单';
            
            self::set_view_status($result['status']);
            self::set_view_message($result['message']);
            return;
        }   
        
        if(empty($shipping_id)){
            $result['status']  = -400;
            $result['message'] = '请选择配送方式';
            
            self::set_view_status($result['status']);
            self::set_view_message($result['message']);
            return;
        }
        if(empty(self::$data['business_config']['shipping_look_up'][$shipping_id])){
            $result['status']  = -400;
            $result['message'] = '请选择有效配送方式';
            
            self::set_view_status($result['status']);
            self::set_view_message($result['message']);
            return;
        }
        $shipping_name = self::$data['business_config']['shipping_look_up'][$shipping_id];
        
        if(empty($shipping_sn)){
            $result['status']  = -400;
            $result['message'] = '请输入配送单号';
            
            self::set_view_status($result['status']);
            self::set_view_message($result['message']);
            return;
        }
    
        $model_background_order = wwy_resource_container::get_model_background_order();
        //var_dump($model_background_order); exit();
        $old_order_info = $model_background_order->find($id);
        //var_dump($old_order_info); exit();
        if(empty($old_order_info)){
            $result['status']  = -400;
            $result['message'] = '该订单不存在';
            
            self::set_view_status($result['status']);
            self::set_view_message($result['message']);
            return;
        }
        
        $update_time = time();
        $order_info = array();
        $order_info['id'] = $id;
        $order_info['shipping_id'] = $shipping_id;
        $order_info['shipping_name'] = $shipping_name;
        $order_info['shipping_sn'] = $shipping_sn;
        $order_info['shipping_time'] = $old_order_info['shipping_time'];
        $order_info['status'] = $old_order_info['status'];
        $order_info['update_time'] = $update_time;
        
        if($old_order_info['status'] == 1){
            $order_info['shipping_time'] = $update_time;
            $order_info['status']       = 2;
        } 
    
        $row_count = $model_background_order->update($order_info);
        //var_dump($row_count); exit();
         
        if($row_count === false){
            $result['status']  = -400;
            $result['message'] = '操作失败';
        }else{
            $result['status']  = 1;
            $result['message'] = '操作成功';
        }
        //var_dump($result); exit();
         
        self::set_view_status($result['status']);
        self::set_view_message($result['message']);
    }
}